package com.wlyuan.open.scanner.repository.impl;

import com.wlyuan.open.scanner.dao.entity.OrderIndexDO;
import com.wlyuan.open.scanner.dao.mapper.OrderIndexMapper;
import com.wlyuan.open.scanner.domain.transport.Transport;
import com.wlyuan.open.scanner.domain.transport.TransportConvert;
import com.wlyuan.open.scanner.repository.WayBillRepository;
import com.wlyuan.open.scanner.utils.enums.order.OrderSrcClassEnum;
import com.wlyuan.open.scanner.utils.enums.order.OrderStatusEnum;
import com.wlyuan.open.scanner.utils.enums.transport.TransportStatusEnum;
import com.wlyuan.open.utils.LongUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * @author lijing
 * @description
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class BillTransportRepository {
    private final OrderIndexMapper orderIndexMapper;
    private final BaseOrderRepository baseOrderRepository;
    private final WayBillRepository wayBillRepository;
    private final BaseCostRepository baseCostRepository;

    public Transport getBillByTransportId(Long transportId) {
        return Optional.ofNullable(transportId).map(id -> {
            var wayBill = wayBillRepository.getByUp(id);
            if (!Optional.ofNullable(wayBill).isPresent()) {
                wayBill = wayBillRepository.getByDown(id);
            }
            return Optional.ofNullable(wayBill).map(data -> parse(data.getUp(), data.getDown())).orElse(null);
        }).orElse(null);
    }

    private Transport parse(Long up, Long down) {
        var data = orderIndexMapper.getById(up);
        if (baseOrderRepository.checkTenant(data.getIndexCreatorcompanyid())) {
            logger.error("运单id：{},租户id:{}", up, data.getIndexCreatorcompanyid());
            return null;
        }
        if (OrderSrcClassEnum.isOrder(data.getIndexSrcclass())) {
            data.setCustomerFinanceStatus(data.getFinanceStatus());
        }
        if (OrderSrcClassEnum.isTransport(data.getIndexSrcclass())) {
            data.setCarrierFinanceStatus(data.getFinanceStatus());
        }
        //基础数据
        data.setIndexSigntime(null);
        data.setIndexReceiptime(null);
        process(data, down);
        var ts = TransportConvert.convert(data, down);

        if (OrderSrcClassEnum.isOrder(data.getIndexSrcclass())) {
            ts.setCustomer(TransportConvert.convertCustomer(data));
        }
        if (OrderSrcClassEnum.isTransport(data.getIndexSrcclass())) {
            //有承运方时
            if (LongUtils.notNullLong(data.getIndexSupplierid()) || LongUtils.notNullLong(data.getIndexSuppliersymbolid()) || LongUtils.notNullLong(data.getIndexDriverid())) {
                //状态16 默认为待签收
                if (OrderStatusEnum.ALREADY.getValue().equals(data.getIndexStatus())) {
                    data.setTransportStatus(TransportStatusEnum.WAIT_SIGN.getValue());
                }
                ts.setCarrier(TransportConvert.convertCarrier(data));
            }
            //无承运方,无下游，状态为待调度
            if (!LongUtils.notNullLong(data.getIndexSupplierid()) && !LongUtils.notNullLong(data.getIndexSuppliersymbolid()) && !LongUtils.notNullLong(data.getIndexDriverid()) && !LongUtils.notNullLong(down)) {
                data.setTransportStatus(OrderStatusEnum.WAIT_DISPATCH.getValue());
            }
            ts.setCustomer(TransportConvert.convertCustomer(data));

            if (LongUtils.notNullLong(down)) {
                var downData = orderIndexMapper.getById(down);
                Optional.ofNullable(downData).ifPresent(d -> {
                    process(d, null);
                    //状态16 默认为待签收
                    if (OrderStatusEnum.ALREADY.getValue().equals(d.getIndexStatus())) {
                        d.setTransportStatus(TransportStatusEnum.WAIT_SIGN.getValue());
                    }
                    //承运方信息特殊处理
                    if (ts.getCarrier() == null) {
                        baseOrderRepository.processCarrier(d, data.getIndexCreatorcompanyid());
                        ts.setCarrier(TransportConvert.convertCarrier(d));
                        //无承运方 和 司机
                        if (ts.getCarrier() != null && ts.getCarrier().getCarrierType() == null) {
                            ts.setCarrier(null);
                        }
                    }
                    d.setCustomerFinanceStatus(d.getFinanceStatus());
                    ts.setCustomer(TransportConvert.convertCustomer(d));
                });
            }
        }
        return ts;
    }

    private void process(OrderIndexDO data, Long down) {
        //地址
        baseOrderRepository.processAddress(data);
        //承运商
        baseOrderRepository.processCarrier(data);
        //补差
        baseOrderRepository.valueAddition(data);
        //费用信息
        baseCostRepository.cost(data, down, false);
    }
}
